package com.example.demo.controller;

import com.example.demo.config.AjaxResult;
import com.example.demo.model.CommentInfo;
import com.example.demo.model.UserInfo;
import com.example.demo.service.UserProfileService;
import com.example.demo.vo.UserProfileVO;
import com.example.demo.vo.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

@RestController
@RequestMapping("/profile")
public class UserProfileController {
    private static final Logger log = LoggerFactory.getLogger(UserProfileController.class);

    @Autowired
    private UserProfileService userProfileService;

    /**
     * 获取用户基本信息
     */
    @GetMapping("/{userId}")
    public AjaxResult getUserProfile(@PathVariable Long userId) {
        try {
            UserProfileVO profile = userProfileService.getUserProfile(userId);
            if (profile == null) {
                return AjaxResult.fail("用户不存在");
            }
            return AjaxResult.success(profile);
        } catch (Exception e) {
            log.error("获取用户信息失败", e);
            return AjaxResult.fail("获取用户信息失败");
        }
    }

    /**
     * 获取用户的商品列表
     */
    @GetMapping("/products/{userId}")
    public AjaxResult getUserProducts(@PathVariable Long userId) {
        try {
            List<ProductVO> products = userProfileService.getUserProducts(userId);
            return AjaxResult.success(products);
        } catch (Exception e) {
            log.error("获取用户商品列表失败", e);
            return AjaxResult.fail("获取用户商品列表失败");
        }
    }

    //获取用户评价
    @GetMapping("/comments/{userId}")
    public AjaxResult getUserComments(@PathVariable Long userId) {
        try {
            List<CommentInfo> comments = userProfileService.getUserComments(userId);
            return AjaxResult.success(comments);
        } catch (Exception e) {
            log.error("获取用户评价失败", e);
            return AjaxResult.fail("获取用户评价失败");
        }
    } 
} 